home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Scripting / Source / StevesParser.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-09  |  2.4 KB  |  84 lines  |  [TEXT/MPCC]

  1. /*---------------------------------------------------------------
  2.     Copyright 1995, Steve Israelson
  3.     
  4.     I own this code.  You are free to use this code in any software
  5.     you want.  You may not sell this source code at all, you can 
  6.     sell your product though.  If you want to include this code
  7.     in any code collection (CD-Roms etc) this is OK as long as
  8.     I get a complimentary copy.
  9.             Steve.
  10.     
  11.     Holds a list of regular expression and matches them to an input
  12.     string returning any parameters and the ID of the expression matched.
  13.     
  14.     Uses PowerPlants LList class, although any list class could
  15.     be used with few changes.
  16. ---------------------------------------------------------------*/
  17. #include "StevesParser.h"
  18. #include "StevesRgExp.h"
  19.  
  20.  
  21. /*---------------------------------------------------------------
  22.     Nothing to construct.
  23. ---------------------------------------------------------------*/
  24. MyParser::MyParser(void)
  25.     {
  26.     }
  27.  
  28. /*---------------------------------------------------------------
  29.     Delete all the expressions from the list.
  30. ---------------------------------------------------------------*/
  31. MyParser::~MyParser()
  32.     {
  33.     RegExp        *theExpr;
  34.     for (int x = 1; x <= expressions.GetCount(); ++x)
  35.         if (expressions.FetchItemAt(x, &theExpr))
  36.             delete theExpr;
  37.     }
  38.  
  39. /*---------------------------------------------------------------
  40.     Pass in the text to be parsed and a list to hold any parameters
  41.     that are found.  Simply trys to match each regular expression
  42.     and returns the expressions ID when it does.
  43. ---------------------------------------------------------------*/
  44. long MyParser::Parse(char *text, LList *params)
  45.     {
  46.     RegExp        *theExpr;
  47.     for (int x = 1; x <= expressions.GetCount(); ++x)
  48.         {
  49.         if (expressions.FetchItemAt(x, &theExpr))
  50.             if (theExpr->Match(text, 0, nil, params))
  51.                 return theExpr->ID;
  52.         }
  53.     return 0;    // no expressions matched
  54.     }
  55.  
  56. /*---------------------------------------------------------------
  57.     Adds an expression to the expression list.  You MUST add the
  58.     more specific expressions first, and the more general last,
  59.     since ".*a.*" will match anything with an "a" in it before
  60.     something like "aardvark" will get matched, so put the ".*a.*"
  61.     in this list last.
  62. ---------------------------------------------------------------*/
  63. void MyParser::AddExpression(char *text, long ID)
  64.     {
  65.     RegExp    *theExpr = RegExp::Parse(text, nil, ID);
  66.     if (theExpr)
  67.         expressions.InsertItemsAt(1, arrayIndex_Last, &theExpr);
  68.     }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.